<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Compare two rows for sorting purposes</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.dbx-compare" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">dbx_compare</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5 &lt; 5.1.0, PECL dbx &gt;= 1.1.0)</p><p class="refpurpose"><span class="refname">dbx_compare</span> &mdash; <span class="dc-title">Compare two rows for sorting purposes</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.dbx-compare-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>dbx_compare</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$row_a</code></span>
   , <span class="methodparam"><span class="type">array</span> <code class="parameter">$row_b</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$column_key</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = DBX_CMP_ASC | DBX_CMP_NATIVE</span></span>
  ] ) : <span class="type">int</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>dbx_compare()</strong></span> is a helper function for 
   <span class="function"><a href="dbx_sort.html" class="function">dbx_sort()</a></span> to ease the make and use of the custom 
   sorting function.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.dbx-compare-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">row_a</code></dt>

     <dd>

      <p class="para">
       First row
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">row_b</code></dt>

     <dd>

      <p class="para">
       Second row
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">column_key</code></dt>

     <dd>

      <p class="para">
       The compared column
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">flags</code></dt>

     <dd>

      <p class="para">
       The <code class="parameter">flags</code> can be set to specify comparison
       direction: 
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara">
          <strong><code>DBX_CMP_ASC</code></strong> - ascending order
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>DBX_CMP_DESC</code></strong> - descending order
         </span>
        </li>
       </ul>
       and the preferred comparison type:
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara">
          <strong><code>DBX_CMP_NATIVE</code></strong> - no type conversion
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>DBX_CMP_TEXT</code></strong> - compare items as strings
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>DBX_CMP_NUMBER</code></strong> - compare items numerically
         </span>
        </li>
       </ul>
       One of the direction and one of the type constant can be combined with
       bitwise OR operator (|).
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.dbx-compare-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns <em>0</em> if the <em>row_a[$column_key]</em>
   is equal to <em>row_b[$column_key]</em>, and <em>1</em>
   or <em>-1</em> if the former is greater or is smaller than the 
   latter one, respectively, or vice versa if the <code class="parameter">flag</code> 
   is set to <strong><code>DBX_CMP_DESC</code></strong>.
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.dbx-compare-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-970">
    <p><strong>Example #1 <span class="function"><strong>dbx_compare()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">user_re_order</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$rv&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dbx_compare</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"parentid"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">DBX_CMP_DESC</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">$rv</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$rv&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dbx_compare</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"id"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">DBX_CMP_NUMBER</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$rv</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$link&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dbx_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">DBX_ODBC</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"db"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"username"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;or&nbsp;die(</span><span style="color: #DD0000">"Could&nbsp;not&nbsp;connect"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dbx_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"SELECT&nbsp;id,&nbsp;parentid,&nbsp;description&nbsp;FROM&nbsp;table&nbsp;ORDER&nbsp;BY&nbsp;id"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;data&nbsp;in&nbsp;$result&nbsp;is&nbsp;now&nbsp;ordered&nbsp;by&nbsp;id<br /><br /></span><span style="color: #0000BB">dbx_sort</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"user_re_order"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;date&nbsp;in&nbsp;$result&nbsp;is&nbsp;now&nbsp;ordered&nbsp;by&nbsp;parentid&nbsp;(descending),&nbsp;then&nbsp;by&nbsp;id<br /><br /></span><span style="color: #0000BB">dbx_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.dbx-compare-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="dbx_sort.html" class="function" rel="rdfs-seeAlso">dbx_sort()</a> - Sort a result from a dbx_query by a custom sort function</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>